const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// 时间转换
const timestampToTime = (future_time, current_time) => {
	if(current_time > future_time) {
		return false
	}
	let s = (future_time - current_time) / 1000
	var h = Math.floor(s / 3600);
	var min = Math.floor(s / 60) % 60;
	var sec = Math.floor(s % 60);
	h = add(h);
	min = add(min);
	sec = add(sec);
	return h + ':' + min + ':' + sec
}

// 添 0
const add = (m) => {
	return m < 10 ? '0' + m : m
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}

function getUrl() {
	let pages = getCurrentPages();
	let current_page = pages[pages.length - 1]
	let route = current_page.route

	let options = current_page.options
	let full_path = route + '?'
	for (let key in options) {
		full_path += `${key}=${options[key]}&`
	}
	full_path = full_path.substring(0, full_path.length - 1)

	return full_path
}

module.exports = {
	formatTime,
	getUrl,
	timestampToTime
}
